<?php
declare (strict_types = 1);

namespace app\controller\backend\finance;

use app\service\logic\common\finance\FinanceCoinService;
use app\validate\common\finance\CoinValidate;
use think\App;
use think\Request;
use think\Response;
use think\response\Json;
use think\db\exception\DbException;
use app\controller\backend\BackendController;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class CoinController extends BackendController
{
    protected $service;

    public function __construct(App $app, FinanceCoinService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 视图
     *
     * @return string
     */
    public function view(): string
    {
        return $this->layout('backend/finance/coin', 'finance_coin');
    }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, CoinValidate::class . '.index');

        $fun = function ($item) {
            $item['type_map'] = $this->service->getTypeMap($item['type']);
            unset($item['belongUser']);
            return $item;
        };
        $result = $this->service->getList($params, $search_fields, $search, ['belongUser'], $fun);
        return amis($result);
    }

    /**
     * 显示资源
     *
     * @param $id
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read($id): Json
    {
        $result = $this->service->getSimpleDetail($id);
        return amis($result);
    }

    // 审核
    public function rr()
    {
        $params = $this->request->param();
        $this->validate($params, CoinValidate::class . '.rr');

        $result = $this->service->rr($params);
        return amis($result);
    }
}
